<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <uieip type="file" value="inc" src="/yqwin/easyui/include.txt" cache="true" minute="1440"></uieip>
    <uieip type="file" value="css" src="/yqwin/css/yqwin.min" cache="false"></uieip>
    <uieip type="file" value="js" src="/yqwin/data/yqwin.min" cache="false"></uieip>
    <!-- <script src="/scripts/gantt/gantt.js"></script>
    <link rel="stylesheet" href="/scripts/gantt/gantt.css" type="text/css">
    <link rel="stylesheet" href="/scripts/gantt/skins/dhtmlxgantt_material.css" type="text/css"> -->
    <style>

    </style>
    <title>yqGantt</title>
</head>

<body>
    <h1>yqGantt</h1>
    <div id="gantt_here" style='width:100%; height:70vh;'></div>
    <input type=button value="getdata" onclick="getdata()">
    <input type=button value="setdata" onclick="setdata()">
    <input type=button value="Toggle grid" onclick="toggleGrid()">
    <input type=button value="Show grid" onclick="toggleGrid2()">
    <input type=button value="Toggle chart" onclick="toggleChart()">
    <input type=button value="Show chart" onclick="toggleChart2()">
    <input type=button value="readonly" onclick="readonly()">
    <input type=button value="enable" onclick="enable2()">
    <input type=button value="resize" onclick="resize()">
    <input type=button value="destroy" onclick="destroy()">
    <input type=button value="reinit" onclick="reinit()">
    <script>
        $(function () {
            $("#gantt_here").yqGantt({
                //扩展 开始
                person_add: true,
                person_class: "fa fa-pencil-square-o",
                person_txt: "扩展编辑",
                person_action: function (btn) {
                    show_edit(btn);
                },
                //扩展 结束
                data: f_data
            });
        })

        function toggleGrid() {
            $("#gantt_here").yqGantt("showgrid", false);
        }

        function toggleGrid2() {
            $("#gantt_here").yqGantt("showgrid", true);
        }

        function toggleChart() {
            $("#gantt_here").yqGantt("showchart", false);
        }

        function toggleChart2() {
            $("#gantt_here").yqGantt("showchart", true);
        }

        function readonly() {
            $("#gantt_here").yqGantt("isEdit", false);
        }

        function enable2() {
            $("#gantt_here").yqGantt("isEdit", true);
        }

        function resize() {
            $("#gantt_here").yqGantt("resize", { width: 500, height: 400 });
        }

        function destroy() {
            $("#gantt_here").yqGantt("destroy");
        }

        function reinit() {
            $("#gantt_here").yqGantt({});
        }


        var f_data = {
            "data": [
                {
                    "uid": "111111",
                    "id": 1,
                    "wbscode": "1",
                    "type": 1,
                    "title": "Project #1",
                    "startdate": "2023/08/01",
                    "enddate": "2023/08/11",
                    "progress": "0.00"
                },
                {
                    "uid": "222222",
                    "id": 2,
                    "wbscode": "1.1",
                    "type": 2,
                    "parent": 1,
                    "title": "Task #1",
                    "startdate": "2023/08/01",
                    "enddate": "2023/08/08",
                    "progress": "0.50",
                    "users": [
                        "1"
                    ],
                    "usersval": "John"
                },
                {
                    "uid": "4mLLi1L2VpV",
                    "id": 3,
                    "wbscode": "1.2",
                    "type": 2,
                    "parent": 1,
                    "title": "Task #2",
                    "startdate": "2023/08/06",
                    "enddate": "2023/08/09",
                    "progress": "0.50",
                    "users": [
                        "2",
                        "3"
                    ],
                    "usersval": "Mike,Anna"
                },
                {
                    "uid": "4mLLi1LcXAZ",
                    "id": 4,
                    "wbscode": "1.3",
                    "type": 1,
                    "parent": 1,
                    "title": "Task #3",
                    "startdate": "2023/08/09",
                    "enddate": "2023/08/11",
                    "progress": "0.80"
                },
                {
                    "uid": "4mLLi1LKVUK",
                    "id": 5,
                    "wbscode": "1.3.1",
                    "type": 2,
                    "parent": 4,
                    "title": "Task #3.1",
                    "startdate": "2023/08/09",
                    "enddate": "2023/08/11",
                    "progress": "0.20",
                    "users": [
                        "1"
                    ],
                    "usersval": "John"
                },
                {
                    "uid": "4mLLi1LneLK",
                    "id": 6,
                    "wbscode": "1.3.2",
                    "type": 3,
                    "parent": 4,
                    "title": "Task #3.2",
                    "startdate": "2023/08/11",
                    "enddate": "2023/08/11"
                }
            ],
            "links": [
                {
                    "form": "2",
                    "to": "3"
                },
                {
                    "form": "3",
                    "to": "5"
                }
            ],
            "user": [
                {
                    "id": "1",
                    "name": "John"
                },
                {
                    "id": "2",
                    "name": "Mike"
                },
                {
                    "id": "3",
                    "name": "Anna"
                }
            ]
        };

        function getdata() {
            f_data = $("#gantt_here").yqGantt("getData");
            console.log(JSON.stringify(f_data, null, 2));
        }

        function setdata() {
            console.log(f_data);
            $("#gantt_here").yqGantt("setData", f_data);
        }


        function show_edit($btn) {
            var f_gantt = $("#gantt_here").yqGantt("getData");
            //console.log(f_gantt);
            var trees = [];
            $.yqListToTree({
                pidVal: null,              //根节点父项属性值
                idName: "id",             //id字段名称
                pidName: "parent",            //pid字段名称
                rows: f_gantt.data           //数据         
            }, function (tree_data) {
                var prevent_e = $.getPreventDefault($btn[0]);
                prevent_e.id = "";
                prevent_e.options = {
                    beforeAdd: function (data, row, callback) {
                        $.yqTreeTolist({
                            pidVal: "0",              //根节点父项属性值
                            idName: "id",             //id字段名称
                            pidName: "parent",            //pid字段名称
                            rows: data           //数据         
                        }, function (_rows) {
                            var id = 0;
                            var startdate = "";
                            var enddate = "";
                            for (var i = 0; i < _rows.length; i++) {
                                var _id = parseInt(_rows[i].id);
                                if (_id > id) {
                                    id = _id;
                                }
                                if (_rows[i].type == 2 &&
                                    !(_rows[i].startdate === undefined)) {
                                    startdate = _rows[i].startdate;
                                    enddate = _rows[i].enddate;
                                }
                            }
                            if (startdate == "") {
                                var now = new Date();
                                var currentDate = now.getDate();
                                startdate = now.Format("yyyy/MM/dd");
                                now.setDate(currentDate + 5);
                                enddate = now.Format("yyyy/MM/dd");
                            }

                            row["id"] = (id + 1);
                            row["type"] = 2;
                            row["title"] = "新任务";
                            row["startdate"] = startdate;
                            row["enddate"] = enddate;
                            row["progress"] = "0";
                            callback(row);
                        });
                    }
                };

                var mdiinfo = $.yqGetWindParams();
                var move_pops = {
                    ajaxUrl: "",
                    prevent_e: prevent_e,
                    mdiinfo: mdiinfo,
                    title: "move",
                    width: 600,
                    height: 400,
                    displayMember: "id",
                    titles: [
                        {
                            "name": "id",
                            "ui_title": "id",
                            "ui_type": 2,
                            "ui_width": 90,
                            "ui_visible": true,
                            "ui_readonly": true,
                            "ui_mask": {}
                        }, {
                            "name": "title",
                            "ui_title": "标题",
                            "ui_type": 2,
                            "ui_width": 220,
                            "ui_visible": true,
                            "ui_readonly": false,
                            "ui_mask": {}
                        }, {
                            "is_func": false,
                            "name": "type",
                            "ui_title": "type",
                            "ui_type": 6,
                            "ui_width": 80,
                            "ui_visible": true,
                            "ui_readonly": false,
                            "ui_mask": {
                                "default": 2,
                                "lists": [
                                    {
                                        "no": "1",
                                        "name": "project"
                                    },
                                    {
                                        "no": "2",
                                        "name": "task"
                                    },
                                    {
                                        "no": "3",
                                        "name": "milestone"
                                    }
                                ]
                            }
                        }, {
                            "is_func": false,
                            "name": "startdate",
                            "ui_title": "开始日期",
                            "ui_type": 4,
                            "ui_width": 80,
                            "ui_visible": true,
                            "ui_readonly": false,
                            "ui_mask": {
                                "format": "yyyy/MM/dd"
                            }
                        }, {
                            "is_func": false,
                            "name": "enddate",
                            "ui_title": "结束日期",
                            "ui_type": 4,
                            "ui_width": 80,
                            "ui_visible": true,
                            "ui_readonly": false,
                            "ui_mask": {
                                "format": "yyyy/MM/dd"
                            }
                        }, {
                            "name": "test",
                            "ui_title": "测试",
                            "ui_type": 2,
                            "ui_width": 220,
                            "ui_visible": true,
                            "ui_readonly": false,
                            "ui_mask": {}
                        }
                    ],
                    data: $.yqTreeSaveInit(tree_data),
                    callback: function (rows) {
                        $.yqTreeTolist({
                            pidVal: "0",              //根节点父项属性值
                            idName: "id",             //id字段名称
                            pidName: "parent",            //pid字段名称
                            rows: rows           //数据         
                        }, function (_rows) {
                            for (var i = 0; i < _rows.length; i++) {
                                if (_rows[i].parent != "0") {
                                    _rows[i].parent = parseInt(_rows[i].parent);
                                }
                                _rows[i].id = parseInt(_rows[i].id);
                            }

                            var rows_ = [];
                            for (var i = 0; i < _rows.length; i++) {
                                var has_row = false;
                                for (var j = 0; j < f_gantt.data.length; j++) {
                                    if (_rows[i].id == f_gantt.data[j].id) {
                                        var _row = $.extend(f_gantt.data[j], _rows[i]);
                                        rows_.push(_row);
                                        has_row = true;
                                        break;
                                    }
                                }
                                if (!has_row) {
                                    rows_.push(_rows[i]);
                                }
                            }
                            f_gantt.data = rows_;

                            //console.log(f_gantt);
                            $("#gantt_here").yqGantt("setData", f_gantt);
                        });
                    }
                };

                if (window.top == window.self) {
                    $.yqTreeEditPopEdit(move_pops);
                }
                else {
                    window.top.$.yqTreeEditPopEdit(move_pops);
                }
            });
        }

    </script>
</body>

</html>